//
//  HomeNavigationVIew.m
//  Arts1
//
//  Created by Mac Jc on 15/10/4.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "HomeNavigationVIew.h"
#import "AllHeader.h"

//添加/取消关注
#define kSetfollow @"http://arts1.cn/app/setfollow"
@implementation HomeNavigationVIew

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        backgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backgroundView];
//        [UIView viewWideForShadowColor:backgroundView];
        [UIView viewShadowColor:backgroundView];
        
        
        [backgroundView addSubview:self.backButton];
        [backgroundView addSubview:self.TapGoodButton];
        [backgroundView addSubview:self.plusFriendButton];
        [backgroundView addSubview:self.shareBTN];
        
    }
    return self;
}

- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _backButton.frame = CGRectMake(8, 8, 28 , 28);
        //        [_backButton setTitle:@"<" forState:(UIControlStateNormal)];
        [_backButton setImage:[UIImage imageNamed:@"backButton"] forState:(UIControlStateNormal)];
        //        [_backButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    return _backButton;
}

- (UIButton *)TapGoodButton{
    if (!_TapGoodButton) {
        _TapGoodButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _TapGoodButton.frame = CGRectMake(SCREEN_WIDTH - 170, 4, 36 , 36);
        [_TapGoodButton setImage:[UIImage imageNamed:@"tabGoodButton"] forState:(UIControlStateNormal)];
        [_TapGoodButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    return _TapGoodButton;
}

- (UIButton *)plusFriendButton{
    if (!_plusFriendButton) {
        _plusFriendButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _plusFriendButton.frame = CGRectMake(SCREEN_WIDTH - 100, 5, 34 , 34);
//        [_plusFriendButton setImage:[UIImage imageNamed:@"plusFriend"] forState:(UIControlStateNormal)];
        [_plusFriendButton addTarget:self action:@selector(addFriend:) forControlEvents:(UIControlEventTouchUpInside)];
        [_plusFriendButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    return _plusFriendButton;
}

- (UIButton *)shareBTN{
    if (!_shareBTN) {
        _shareBTN = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _shareBTN.frame = CGRectMake(SCREEN_WIDTH - 44, 5, 34 , 34);
        [_shareBTN setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
        [_shareBTN setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }
    return _shareBTN;
}

- (void)setStatus:(int)status
{
    _status = status;
    
    //如果是已经关注
    if (_status) {
        [_plusFriendButton setBackgroundImage:[UIImage imageNamed:@"plusHadFriend"] forState:UIControlStateNormal];
//        [_collectionButton setTitle:@"取消收藏" forState:(UIControlStateNormal)];share
    }
    else
    {
        [_plusFriendButton setBackgroundImage:[UIImage imageNamed:@"plusFriend"] forState:UIControlStateNormal];
    }
}

//添加关注
- (void)addFriend:(UIButton *)addFriend
{
    addFriend.selected = !addFriend.isSelected;
    if (_status)
    {
        [_plusFriendButton setBackgroundImage:[UIImage imageNamed:@"plusFriend"] forState:UIControlStateNormal];
        //取消关注
        [self requestAddFriend:@"cancel"];
        _status = 0;
    }
    else
    {
        [_plusFriendButton setBackgroundImage:[UIImage imageNamed:@"plusHadFriend"] forState:UIControlStateNormal];
        //添加关注
        [self requestAddFriend:@"add"];
        _status = 1;
        
    }
}

//添加/取消关注
- (void)requestAddFriend:(NSString *)act
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = nil;
    if (uid && key && _userID) {
        pardic = @{@"type":@"user",
                   @"uid":uid,
                   @"key":key,
                   @"act":act,
                   @"userId":_userID};
    }
    
    if ([uid isEqualToString:_userID]) {
        [MBProgressHUD show:@"不能关注自己" icon:nil view:self];
        return;
    }
    
    [manage GET:kSetfollow parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             
             // 请求成功的时候调用这个block
             NSLog(@"数据请求-------------------%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}
@end
